package com.example.leetcode;

/**
 * @Date 2023/2/23
 * @Author Jonathan
 */
public class Code07_Solution {
    public int reverse(int x) {
        // 判断一个数是不是负数
        boolean neg = ((x >>> 31) & 1) == 1;
        x = neg ? x : -x;
        int m = Integer.MIN_VALUE / 10;
        int o = Integer.MIN_VALUE % 10;
        int res = 0;
        while (x != 0) {
            // 本题的关键是判断是否是溢出
            // 这个是技能点
            if (res < m || (res == m && x % 10 < o)) {
                return 0;
            }
            res = res * 10 + x % 10;
            x /= 10;
        }
        return neg ? res : Math.abs(res);
    }

    public static void main(String[] args) {
        Code07_Solution solution = new Code07_Solution();
        System.out.println(solution.reverse(123));
    }
}
